<%@ page language="java" contentType="text/html; charset=GBK"  
    pageEncoding="GBK"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<link href="../style/stylesheet.css" rel="stylesheet" type="text/css" />  
<title>PollMaster: Add User</title>  

<script type="text/javascript" >
	function createXMLHttpRequest(){
	  // See http://en.wikipedia.org/wiki/XMLHttpRequest
	  // Provide the XMLHttpRequest class for IE 5.x-6.x:
	  if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
	    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
	    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
	    try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
	    try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
	    throw new Error( "This browser does not support XMLHttpRequest." )
	  };
	  return new XMLHttpRequest();
	}
	
	var AJAX = createXMLHttpRequest();
	
	function handler() {
		if(AJAX.readyState == 4 && AJAX.status == 200) {
			var json = eval('(' + AJAX.responseText +')');
			if(json.status != "bad"){
				if(json.status == "good"){
					document.getElementById("write").innerHTML = "User added successfully!";
				}else{
					console.log(json.status);
					document.getElementById("statustext").innerHTML = "Query made! Question is:";
					document.getElementById("statustext").innerHTML += " closed :( ";
					if(!(json.optionid == -1)){
						//each JSON object received will contain status, optionid and correct(true/false)
						console.log(json.optionid+"///");
						if(json.correct == 1 ){
							console.log("here3");
							var optionid = "option";
							optionid += Integer.parseInt(json.optionid);
							console.log(optionid);
							document.getElementById(optionid).style.display = "block";
						}
					}else{
						setTimeout('checkQuestionStatus()', 1000);
						console.log("or here"+json.optionid);
						if(json.optionid == -1){
							//setTimeout('checkQuestionStatus()', 1000);
						}else{
							var els = document.getElementsByClassName("option");
							for(var i=0; i < els.length; i++){
								var e = els[i].id;
								console.log(i);
								//call a getOptionIndicator(optionid), which will eventually call this handler
								getOptionIndicator(e, "11");
							}
						}
					}
				}
			}else {
				alert("SQL query not successful.");
			}
		}else if (AJAX.readyState == 4 && AJAX.status != 200) {
			alert("AJAX query was unsuccessful.");
		}
	}
	
	function addUser(form){
		var params = "username=";
		params +=form["username"].value;
		params +="&password="+form["password"].value;
		params +="&generic="+form["generic"].value;
		params +="&email="+form["email"].value;
		params +="&fname="+form["fname"].value;
		params +="&lname="+form["lname"].value;
		document.getElementById("write").innerHTML = params;
		AJAX = createXMLHttpRequest();
		AJAX.onreadystatechange = handler;
		AJAX.open("POST", "PollMaster/AddUserServlet");
		AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		AJAX.send(params);
	}
</script> 

</head>
<body>
<div id="write"></div>
<div class="content">
	<h3>PollMaster: Add User</h3>
		<form action="AddUserServlet" method="post"">
			<label for="username">Username</label><br/><input type="text" id="username"/><br/>
			<label for="password">Password</label><br/><input type="text" id="password"/><br/>
			<label for="generic">Generic Account</label><br/>
			<select id="generic">
				<option value="true">Yes</option>
				<option value="false">No</option>
			</select><br/>
			<label for="email">Email</label><br/><input type="text" id="email"/><br/>
			<label for="fname">First name</label><br/><input type="text" id="fname"/><br/>
			<label for="lname">Last name</label><br/><input type="text" id="lname"/><br/>
			<input type="submit" value="Add User"/>
		</form>
		</div>
</body>  
</html>  
